//
// Created by ftd on 2021/8/18.
//

#include "handle/net_msg_event_handle.h"
#include "room/room_manager.h"
#include "demo.h"

NetMsgEventHandle& NetMsgEventHandle::Instance() {
    static NetMsgEventHandle instance;
    return instance;
}

NetMsgEventHandle::NetMsgEventHandle()
        : total_event_handle_cnt_(0) {
}

NetMsgEventHandle::~NetMsgEventHandle() {

}

void NetMsgEventHandle::RegisterEventHandle() {
    event_handle_funcs_[kOpenRoom] = std::bind(&NetMsgEventHandle::HandleOpenRoomOperate, this,
                                              std::placeholders::_1, std::placeholders::_2);
    event_handle_funcs_[kModifyRoom] = std::bind(&NetMsgEventHandle::HandleModifyRoomOperate, this,
                                                std::placeholders::_1, std::placeholders::_2);
    event_handle_funcs_[kRestartRoom] = std::bind(&NetMsgEventHandle::HandleRestartRoomOperate, this,
                                                 std::placeholders::_1, std::placeholders::_2);
    event_handle_funcs_[kDeleteRoom] = std::bind(&NetMsgEventHandle::HandleDeleteRoomOperate, this,
                                                std::placeholders::_1, std::placeholders::_2);
    event_handle_funcs_[kCloseRoom] = std::bind(&NetMsgEventHandle::HandleCloseRoomOperate, this,
                                               std::placeholders::_1, std::placeholders::_2);
}

void NetMsgEventHandle::Update() {
    static int update_cnt = 0;
    update_cnt++;
    fprintf(stdout, "TPS:%ld\n", total_event_handle_cnt_ .load(std::memory_order_relaxed) / update_cnt);

}

bool NetMsgEventHandle::EventHandle(int msg_id, void* msg, int msg_size) {
    if (msg_id >= 0 && msg_id < kMsgIdCnt) {
        if (Demo::Instance().IsStressTest()) {
            total_event_handle_cnt_.fetch_add(1, std::memory_order_relaxed);
        }
        return event_handle_funcs_[msg_id](msg, msg_size);
    } else {
        fprintf(stderr, "msgId out of range\n");
    }
    return false;
}

bool NetMsgEventHandle::HandleOpenRoomOperate(void *msg, int msg_size) {
    if (sizeof(OpenRoomOperate) != msg_size) {
        fprintf(stderr, "%s:msg size unequal struct OpenRoomOperate size \n", __FUNCTION__);
        return false;
    }
    auto open_oper = static_cast<OpenRoomOperate*>(msg);
//    fprintf(stdout, "%s\n", open_oper->open_info);
    RoomManager::Instance().OnOpenRoomOperate(open_oper);
    return true;
}

bool NetMsgEventHandle::HandleModifyRoomOperate(void *msg, int msg_size) {
    if (sizeof(ModifyRoomOperate) != msg_size) {
        fprintf(stderr, "%s:msg size unequal struct ModifyRoomOperate size \n",__FUNCTION__ );
        return false;
    }
    auto modify_oper = static_cast<ModifyRoomOperate*>(msg);
//    fprintf(stdout, "%s\n", modify_oper->modify_info);
    RoomManager::Instance().OnModifyRoomOperate(modify_oper);
    return true;
}

bool NetMsgEventHandle::HandleRestartRoomOperate(void *msg, int msg_size) {
    if (sizeof(RestartRoomOperate) != msg_size) {
        fprintf(stderr, "%s:msg size unequal struct RestartRoomOperate size \n",__FUNCTION__ );
        return false;
    }
    auto restart_oper = static_cast<RestartRoomOperate*>(msg);
//    fprintf(stdout, "%s\n", restart_oper->restart_info);
    RoomManager::Instance().OnRestartRoomOperate(restart_oper);
    return true;
}

bool NetMsgEventHandle::HandleDeleteRoomOperate(void *msg, int msg_size) {
    if (sizeof(DeleteRoomOperate) != msg_size) {
        fprintf(stderr, "%s:msg size unequal struct DeleteRoomOperate size \n",__FUNCTION__ );
        return false;
    }
    auto delete_oper = static_cast<DeleteRoomOperate*>(msg);
//    fprintf(stdout, "%s\n", delete_oper->delete_info);
    RoomManager::Instance().OnDeleteRoomOperate(delete_oper);
    return true;
}

bool NetMsgEventHandle::HandleCloseRoomOperate(void *msg, int msg_size) {
    if (sizeof(CloseRoomOperate) != msg_size) {
        fprintf(stderr, "%s:msg size unequal struct CloseRoomOperate size \n",__FUNCTION__ );
        return false;
    }
    auto close_oper = static_cast<CloseRoomOperate*>(msg);
//    fprintf(stdout, "%s\n", close_oper->close_info);
    RoomManager::Instance().OnCloseRoomOperate(close_oper);
    return true;
}
